<?php
namespace Teachers\Controller;
use Think\Auth;

use Think\Controller;

class CommonController extends Controller
{
	
	public static $Departments_type;
        
        public function _initialize()
	{
		$auth=new Auth();
		$uid=$_SESSION['onethink_home']['user_auth']['uid'];
		$auth_bool=$auth->check('teachers', $uid);
                $school_id=$_SESSION['onethink_home']['school_id'];
        if(!$uid){
        	$this->error('您还未登录，请先登录!',U('/User/login'),1);
        	
        }    
            
	 	 if($auth_bool==false && is_administrator($uid)==false)
	    {
	    	$this->error('您不是教师用户，请先退出然后登录教师用户!',U('/User/login'),1);
	    } 
	    if(is_administrator($uid)){
	    	$school_id=I('get.school_id');
	    }else{
	    	$school_id=$_SESSION['onethink_home']['school_id'];
	    }
	    
	}
        public function showschoolID(){
        	$school_id = $_SESSION['onethink_home']['school_id'];
        	if($school_id){
        		return $_SESSION['onethink_home']['school_id'];
        	}else{
        		$this->error('您尚未登录，请先登录后操作！',U('/User/login'),1);
        	}
            
        }
	/**
	 * 获取当前教师用户所在学校各项类别信息
	 * param school_id 学校ID
	 * param category_id 一级分类ID
	 * param pid	子分类ID
	 * */
	function showSchoolArticle($school_id,$category_id,$pid,$page,$pagesize)
	{
		$document=D('Document');
		$map['school_id']=$school_id;
		$map['category_id']=$category_id;
		$map['pid']=$pid;
		$map['status']=1;	//查询未删除的信息
		$document_data=$document->where($map)->order('create_time desc')->page($page,$pagesize)->select(); 
		return $document_data;
	}
	
	function getArticleById($id){
		if($id){
			$document=D('Document');
			$map['id']=$id;
			$article_data=$document->where($map)->find();
			return $article_data;
		}
	}
	 
	function getArticleCount($school_id,$category_id,$pid){
		$document=D('Document');
		$map['school_id']=$school_id;
		$map['category_id']=$category_id;
		$map['pid']=$pid;
		$map['status']=1;	//查询未删除的信息
		$count=$document->where($map)->count(); 
		return $count;
	}
	
	/**
	 * 新增学校各项类别信息
	 * 
	 * */
	function addArticle($post_data)
	{	
		if($post_data['school_id'] && $post_data['category_id'])
		{	
				$document=M('document');
				$article=M('document_article');
				$data = array(									//document表更新数据
					'school_id'		=> $post_data['school_id'],		//学校ID
					'title'			=> $post_data['title'],			//文档标题
					'description'	=> $post_data['description'],	//文档描述
					'category_id'	=> $post_data['category_id'],	//文档分类ID
					//'model_id'		=>I('post.model_id'),	 	//内容模型ID
					//'type'			=>I('post.type'),	 		//内容类型ID
					//'link_id'		=> I('post.link_id'),	 	//链接ID
					//'level'			=> I('post.level'),	 		//级别
					'create_time'	=> time(),	//创建时间
					//'update_time'	=> time(),	//更新时间
					'pid'			=> 0,			//子分类ID
					'status'		=> 1,
				);
				$article_id=$document->add($data);
			
				if($article_id)
				{
					$contentdata = array(		
						'id'		=> $article_id,
						//'parse'		=> I('post.parse'),		//解析类型
						'content'	=> $post_data['content'],		//文章内容
						//'template'	=> I('post.template'),		//详情页显示模板
						//'bookmark'	=> I('post.bookmark'),		//文档收藏数
					);
					$articleflag=$article->add($contentdata);
					/*if($articleflag)
					{
						$this->error('“'.I('post.title').'” 新增失败！');
					}*/
				}
			
		}
	}
	
	
	/**
	 *更新学校各项类别信息
	 * 
	 * */
	function updateArticle($data_post)
	{
		$document=M('document');
		$article=M('document_article');
		
		if(!empty($data_post['school_id']) && !empty($data_post['article_id']))
		{
			$where['school_id']=$data_post['school_id'];
			$where['category_id']=$data_post['category_id'];
			$where['pid']=$data_post['pid']?$data_post['pid']:0;
			$where['id']=$data_post['article_id'];
			$article_where['id']=$data_post['article_id'];
			$data = array(									//document表更新数据
					//'school_id'		=> $data_post['school_id'],		//学校ID
					'title'			=> $data_post['title'],			//文档标题
					'description'	=> $data_post['description'],	//文档描述
					//'category_id'	=> $data_post['category_id'],	//文档分类ID
					//'model_id'	=> $data_post['model_id'],	 	//内容模型ID
					//'type'		=> $data_post['type'],	 		//内容类型ID
					//'link_id'		=> $data_post['link_id'],	 	//链接ID
					//'level'		=> $data_post['level'],	 		//级别
					//'create_time'	=> $data_post['create_time'],	//创建时间
					'update_time'	=> time(),	//更新时间
					//'status'		=> $data_post['status'],		//状态位
					//'pid'			=> $data_post['pid'],			//子分类ID
					//'status'		=> $data_post['status'],
				);
			$contentdata = array(		
						//'id'		=> $data_post['article_id'],
						//'parse'		=> I('post.parse'),		//解析类型
						'content'	=> I('post.content'),		//文章内容
						//'template'	=> I('post.template'),		//详情页显示模板
						//'bookmark'	=> I('post.bookmark'),		//文档收藏数
			);
			$flag=$document->where($where)->save($data);			//更新document表
			if($flag)
			{
				$article->where($article_where)->save($contentdata);	//更新document_article表
			}else{
				$this->error ('“'.$_POST['title'].'” 更新失败！');
			}
		}
		
	}
	
	
	 /**
	  * 删除各项类别信息
	  *  假删除 更新状态位为-1
	  **/
	function deleteArticle($id)
	{
		$article_id=I('post.id');
		if(!empty($article_id) && $article_id>0)
		{
			$document=M('document');
			$where['id']=$article_id;
			$data=array
			(
				'status' => '-1'
			);
			if(!($document->where($where)->save($data)))
			{
				$this->error('删除失败！');
			}
			
//			if($document->where($where)->delete())
//			{
//				$article->where($where)->delete();
//			}
//			else
//			{
//				$this->error = '删除失败！';
//			}
			
		}
		
		
	}
        /*

         * deleteasks  删除问答
         * 
         *          */
        function deleteasks($ask_id){
            $ask_id=I('post.ask_id');
            if(!empty($ask_id) && $ask_id>0)
		{
			$ask=M('SchoolAsk');
			$where['ask_id']=$ask_id;
			$data=array
			(
				'state' => '-1'
			);
			if(!($ask->where($where)->save($data)))
			{
				$this->error('删除失败！');
			}
		}
        }
        /**
	 * 获取当前教师用户所在学校的问题
	 * param school_id 学校ID
         * param state   0未回答 1已回答
	 * */
	function showAskArticle($school_id,$grade,$page,$pagesize,$state=3)
	{
		$ask=D('SchoolAsk');
		$map['school_id']=$school_id;
                $map['grade']=$grade;
                if($state !=3){
                    $map['state']=$state;
                }	
		$data=$ask->where($map)->order('timeadd desc')->page($page,$pagesize)->select(); 
//                echo $ask->_sql();
		return $data;
	}
        /**
	 * 获取当前教师用户所在学校的问题的分页
	 * param school_id 学校ID
         * param state   0未回答 1已回答
	 * */
        function getaskCount($school_id,$grade,$state=3){
		$document=D('SchoolAsk');
		$map['school_id']=$school_id;
                $map['grade']=$grade;
		 if($state !=3){
                    $map['state']=$state;
                }
		$count=$document->where($map)->count(); 
		return $count;
	}
        /*
         * 
         * 修改问题
         *
         *           */
        function updateasks($data,$grade){
            $ask=M('SchoolAsk');
            $info=$_SESSION['onethink_home'];
            $data['teacher_id']=$info['user_id'];
            $data['teacher_name']=$info['user_name'];
            $data['timereply']=time();
            $data['state']=1;
            if($ask->save($data)){
             $this->redirect("Teachers/Ask/index",array('grade'=>$grade));
            }
            else
            {
                    $this->error('删除失败！');
            }
            
            
        }
        

}